package com.loongxun.playwithyou.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.widget.ListView;
import android.widget.TextView;

import com.loongxun.playwithyou.ApiInterface;
import com.loongxun.playwithyou.R;
import com.loongxun.playwithyou.bean.Member;
import com.loongxun.playwithyou.bean.SendRequest;
import com.loongxun.playwithyou.network.MyHttpClientRespenseHandler;
import com.loongxun.playwithyou.network.nHttpClient;
import com.loongxun.playwithyou.ui.adapter.EvaluateAdapter;
import com.loongxun.playwithyou.util.ToastView;
import com.loongxun.playwithyou.view.pullableview.PullToRefreshLayout;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 全部评价
 * Created by Administrator on 2017/8/25.
 */
public class EvaluateActivity extends Activity {

    //标题
    private TextView tv_top_title;
    private Context context;
    private String memberId;
    private SendRequest send;
    private int pageNo = 1;
    private boolean isPageNoMore = false;
    private List<Member> evaluateList = new ArrayList<>();
    private EvaluateAdapter mAdapter;

    private ListView listView;
    private PullToRefreshLayout ptrl;
    private boolean isFirstIn = true;
    private TextView loadstate_tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_eval_list);
        context = EvaluateActivity.this;

        memberId = getIntent().getStringExtra("member_id");
        pageNo = 1;
        initData(pageNo);

        findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        tv_top_title = (TextView) findViewById(R.id.tv_top_title);
        tv_top_title.setText("全部评价");

        ptrl = ((PullToRefreshLayout) findViewById(R.id.refresh_view));
        loadstate_tv = (TextView) findViewById(R.id.loadstate_tv);
        ptrl.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(final PullToRefreshLayout pullToRefreshLayout) {
                // 下拉刷新操作
                new Handler() {
                    @Override
                    public void handleMessage(Message msg) {
                        pageNo = 1;
                        initData(pageNo);
                        // 千万别忘了告诉控件刷新完毕了哦！
                        pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
                    }
                }.sendEmptyMessageDelayed(0, 3000);
            }

            @Override
            public void onLoadMore(final PullToRefreshLayout pullToRefreshLayout) {
                // 下拉刷新操作
                new Handler() {
                    @Override
                    public void handleMessage(Message msg) {
                        if (isPageNoMore) {
                            pageNo = pageNo + 1;
                            initData(pageNo);
                        } else {
                            loadstate_tv.setText("已加载全部数据");
                        }
                        // 千万别忘了告诉控件刷新完毕了哦！
                        pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
                    }
                }.sendEmptyMessageDelayed(0, 3000);

            }
        });
        listView = (ListView) findViewById(R.id.content_view);
        ptrl.autoRefresh();
    }

    //列表数据访问
    private void initData(int pageNo) {
        try {
            send = new SendRequest();
            send.memberId = memberId;
            send.pageNo = pageNo;
            //陪玩人员详情
            nHttpClient.getAll(ApiInterface.EVALUATE, send.toOrderMemberJson().toString(), indexHandler);
        }catch (Exception e){
            ToastView toast = new ToastView(context, "参数错误");
            toast.show();
        }
    }

    private MyHttpClientRespenseHandler indexHandler = new MyHttpClientRespenseHandler() {

        @Override
        public void success(JSONObject json) {
            Integer succeed = json.optJSONObject("status").optInt("succeed", 0);
            JSONArray itemArray;
            if (succeed == 1) {
                try {
                    itemArray = json.getJSONObject("data").getJSONArray("evaluateList");
                    if (pageNo == 1) {
                        evaluateList.clear();
                    }
                    for(int i = 0;i < itemArray.length();i++) {
                        JSONObject evaluateObject = itemArray.getJSONObject(i);
                        Member member1 = new Member();
                        member1.setEvaluateInfo(evaluateObject.getString("evaluateInfo"));
                        member1.setCreateDate(evaluateObject.getString("create_date"));
                        String user = evaluateObject.getString("playUser");
                        if (!"null".equals(user) && !"".equals(user)) {
                            JSONObject userObject = evaluateObject.getJSONObject("playUser");
                            member1.setMember_name(userObject.getString("user_name"));
                            member1.setMember_img(userObject.getString("user_photo"));
                        }else {
                            member1.setMember_name("");
                            member1.setMember_img("");
                        }
                        evaluateList.add(member1);
                    }
                    isPageNoMore = json.getJSONObject("data").getBoolean("isPageNoMore");
                    if(mAdapter == null) {
                        mAdapter = new EvaluateAdapter(context,evaluateList);
                        listView.setAdapter(mAdapter);
                    }else {
                        mAdapter.setList(evaluateList);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }else {
                String error_desc = json.optJSONObject("status").optString("error_desc");
                new ToastView(context, error_desc).show();
            }
        }

        @Override
        public void faile(String json) {
            new ToastView(context, json).show();
        }
    };

}
